package com.tudouji.project.flow.controller;

import com.flow.service.pojo.BasePojo;
import com.flow.service.pojo.ResultParam;
import com.flow.service.pojo.result.FlowResult;
import com.flow.service.process.service.IFlowBaseService;
import com.tudouji.common.utils.SecurityUtils;
import com.tudouji.common.utils.poi.ExcelUtil;
import com.tudouji.framework.aspectj.lang.annotation.Log;
import com.tudouji.framework.aspectj.lang.enums.BusinessType;
import com.tudouji.framework.interceptor.annotation.RepeatSubmit;
import com.tudouji.framework.web.controller.BaseController;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.framework.web.page.TableDataInfo;
import com.tudouji.project.flow.domain.Operationlogs;
import com.tudouji.project.flow.service.IOperationlogsService;
import com.tudouji.project.flow.utils.FlowProcessUtil;
import com.tudouji.project.harvest.service.impl.BaseServiceFactory;
import com.tudouji.project.system.domain.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 操作日志Controller
 *
 * @author tudouji
 * @date 2021-04-29
 */
@Slf4j
@RestController
@RequestMapping("/flow/operationlogs")
public class OperationlogsController extends BaseController
{
    @Autowired
    private IOperationlogsService operationlogsService;

    @Autowired
    private FlowProcessUtil processUtil;


    /**
     * 流程消息回复
     */
    @Log(title = "消息回复", businessType = BusinessType.INSERT)
    @PostMapping("/reply")
    public AjaxResult reply(@RequestParam(value="fileIds") String fileIds,Long flowId,Long orderId, String content){
        SysUser user= SecurityUtils.getLoginUser().getUser();
        BasePojo base = null;
        //需要根据flowId  补全  base基础信息
        IFlowBaseService baseService= BaseServiceFactory.getFlowBaseServiceById(flowId);
        log.info("====reply we get baseService :" + baseService);
        if (baseService!=null) {
            base = baseService.getBaseById(orderId);
            base.prepareForProcess();
        }
        //20230314 换成flowProcessProxy.comment
        FlowResult result = processUtil.reply2(flowId, fileIds, content, user, base);

        if(result.getMsg().equals(ResultParam.SUCCESS)){
            return AjaxResult.success();
        }else{
            return AjaxResult.error("系统错误");
        }
    }

    /**
     * 查询操作日志列表
     */
    @PreAuthorize("@ss.hasPermi('flow:operationlogs:list')")
    @GetMapping("/list")
    public TableDataInfo list(Operationlogs operationlogs)
    {
        startPage();
        List<Operationlogs> list = operationlogsService.selectOperationlogsList(operationlogs);
        return getDataTable(list);
    }

    /**
     * 导出操作日志列表
     */
    @PreAuthorize("@ss.hasPermi('flow:operationlogs:export')")
    @Log(title = "操作日志", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Operationlogs operationlogs)
    {
        List<Operationlogs> list = operationlogsService.selectOperationlogsList(operationlogs);
        ExcelUtil<Operationlogs> util = new ExcelUtil<Operationlogs>(Operationlogs.class);
        return util.exportExcel(list, "operationlogs");
    }

    /**
     * 获取操作日志详细信息
     */
    @PreAuthorize("@ss.hasPermi('flow:operationlogs:query')")
    @GetMapping(value = "/{autoid}")
    public AjaxResult getInfo(@PathVariable("autoid") Long autoid)
    {
        return AjaxResult.success(operationlogsService.selectOperationlogsById(autoid));
    }

    /**
     * 新增操作日志
     */
    @PreAuthorize("@ss.hasPermi('flow:operationlogs:add')")
    @Log(title = "操作日志", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Operationlogs operationlogs)
    {
        return toAjax(operationlogsService.insertOperationlogs(operationlogs));
    }

    /**
     * 修改操作日志
     */
    @PreAuthorize("@ss.hasPermi('flow:operationlogs:edit')")
    @Log(title = "操作日志", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Operationlogs operationlogs)
    {
        return toAjax(operationlogsService.updateOperationlogs(operationlogs));
    }

    /**
     * 删除操作日志
     */
    @PreAuthorize("@ss.hasPermi('flow:operationlogs:remove')")
    @Log(title = "操作日志", businessType = BusinessType.DELETE)
	@DeleteMapping("/{autoids}")
    public AjaxResult remove(@PathVariable Long[] autoids)
    {
        return toAjax(operationlogsService.deleteOperationlogsByIds(autoids));
    }


    @Log(title = "消息撤回NEW", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping("/cancelComment")
    public AjaxResult cancelComment(Long id){
        SysUser user= SecurityUtils.getLoginUser().getUser();
        FlowResult result = processUtil.cancelComment(id, user);
        if(result.getMsg().equals(ResultParam.SUCCESS)){
            return AjaxResult.success();
        }else{
            return AjaxResult.error("系统错误");
        }
    }

    @Log(title = "消息撤回", businessType = BusinessType.INSERT)
    @PostMapping("/cancelReply/{id}")
    public AjaxResult cancelReply(@PathVariable("id") Long id){
        SysUser user= SecurityUtils.getLoginUser().getUser();
        FlowResult result = processUtil.cancelReply(id, user);
        if(result.getMsg().equals(ResultParam.SUCCESS)){
            return AjaxResult.success();
        }else{
            return AjaxResult.error("系统错误");
        }
    }
}
